<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/6/13
 * Time: 下午5:15
 */
class Robot extends MY_Cron_Controller
{
    /**
     * 计算机器人观看直播人数
     *
     * php /data/www/shikee/web/cli.php direct_seeding/Robot/ai_watch
     */
    public function ai_watch()
    {
        $where = array
        (
            'is_public'        => DIRECT_SEEDING_IS_PUBLISH_OPEN,
            'status'           => DS_STATUS_DOING,
            'real_end_time >=' => date('Y-m-d H:i:59', strtotime("-5 minute")),
            'amount<'          => 100,
        );


        $this->load->model('direct_seeding/Model_direct_seeding_join_log', 'dsjl');
        $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
        $data = $this->ds->getAllLists($where);
        if(!$data)
        {
            return ;
        }


        foreach ($data as $v)
        {
            //当前直播间人数
            $number         = $this->dsjl->online_number($v['ds_id']);
            $current_number = (int)$number['data']['number'];


            if(0 < $v['expect_join_num'])//已设定人数
            {
                $remainder_time     = ceil( ( strtotime($v['real_end_time']) - TIMESTAMP ) / 60 ) - 5;//剩余分钟数 减去最后五分钟不进机器人
                $minute_join_number = ceil((ceil($v['expect_join_num']) - $current_number) / $remainder_time);//每分钟需要加入的人数
                $minute_join_number = ceil($minute_join_number / 7.3);


                $join_number = mt_rand( ($minute_join_number - 5), ($minute_join_number + 20) );// * $remainder_time;
                $join_number = (0 < $join_number) ? $join_number : 0;//处理负数
            }
            else//自动增加人数
            {
                $join_number = mt_rand(10, 40);
            }


            $join_number = (1 > $current_number) ? 50 : $join_number;//处理初始加入人数
            $join_number = $current_number > 200000 ? 0 : $join_number;


            if (!$join_number)
            {
                continue;
            }


            //echo time()."\n";
            //echo "/usr/local/php/bin/php /data/www/shikee/web/cli.php direct_seeding/Robot/ai_watch_join {$v['ds_id']} $join_number > /dev/null &";exit;
            exec("/usr/local/php/bin/php /data/www/shikee_old/web/cli.php direct_seeding/Robot/ai_watch_join {$v['ds_id']} $join_number > /dev/null &");
            //echo time()."\n";
        }
    }


    /**
     * 机器人加入观看直播
     *
     * 目前单场直播都以进程以单位进行人数的增加。
     * 后期优化思路可以以常驻服务(NodeJS、PHP扩展等)，用线程去实现。
     *
     * php /data/www/shikee/web/cli.php direct_seeding/Robot/ai_watch_join
     */
    public function ai_watch_join()
    {
        //echo time()."\n";
        set_time_limit(0);


        $ds_id       = (int)$_SERVER['argv'][2];
        $join_number = (int)$_SERVER['argv'][3];


        if (!$ds_id || !$join_number)
        {
            return ;
        }


        $this->load->model('direct_seeding/Model_direct_seeding_join_log', 'dsjl');
        $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
        $ds_item = $this->ds->getById($ds_id);
        if (empty($ds_id))
        {
            return ;
        }


        $uid = range(105001, 109000);//获取机器人用户ID
        shuffle($uid);

        $micro_seconds = 50 * 1000000;//转换成微秒
        $sleep_time    = ceil($micro_seconds / $join_number);

        //循环加入人数
        for($i = 0; $i<$join_number; $i++)
        {
            $status = $this->dsjl->join($uid[$i], $ds_id, 0, DIRECT_SEEDING_WATCH_PAY_TYPE_AI);


            usleep($sleep_time);
        }
        echo time()."\n";
    }



    /**
     * AI聊天
     *
     * php /data/www/shikee/web/cli.php direct_seeding/Base/ai_chat
     */
    public function ai_chat()
    {
        set_time_limit(0);

        $this->load->service('message/service_message_im');
        $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
        $this->load->model('direct_seeding/Model_direct_seeding_join_log', 'dsjl');
        $this->load->model('user/Model_user', 'u');


        $where = array
        (
            'is_public'    => DIRECT_SEEDING_IS_PUBLISH_OPEN,
            'status'       => DS_STATUS_DOING,
            //'real_start_time >=' => date('Y-m-d H:i:59', strtotime("-10 minute")),
        );

        $data = $this->ds->getAllLists($where);

        if(!$data)
        {
            exit;
        }

        foreach ($data as $v)
        {

            $ai_log = $this->dsjl->getOne(array('ds_id'=>$v['ds_id'], 'pay_type'=>DIRECT_SEEDING_WATCH_PAY_TYPE_AI));

            if(!$ai_log)
            {
                continue;
            }

            $user_info = $this->u->getById($ai_log['uid']);

            $ai_chat = json_encode(array('uid'=>$user_info['uid'], 'nickname'=>str_replace("\r", '', $user_info['nickname']), 'message'=>'赞'));


            $data = array
            (
                'chat_room_num' => $v['chat_room_num'],
                'content'       => '@MSMK@LIVE_CHAT_MESSAGE@'.$ai_chat,
            );
            $this->service_message_im->group_send($data);
        }
    }
}